In this set of exercises, we will 1) make sure that you can use Git from within RStudio and 2) clone and work on the GitHub repository you have created in the previous session.

Note: You can find the solutions for this exercise as well as the following ones in the solutions folder within the workshop materials.

1

If you have not already done so, the first thing you should do is check whether RStudio can detect Git.
The easiest way to do this in RStudio is via the Global Options.
You can check whether RStudio can detect your local Git installation via Tools -> Global Options -> Git/SVN. If the menu displays the location of a Git executable, everything should work. If this is not the case, you may need to point RStudio to the location of your Git executable. You can find some further information on this in the troubleshooting section RStudio, Git, GitHub Hell of Happy Git and GitHub for the useR.

2

You may also have done this already, but in case not, now is the time to set up a GitHub personal access token (PAT) for use with R and RStudio.
There are two ways of doing this: Through the GitHub web interface or via a command from an R package.
# Option 1: While logged in to your GitHub account go to https://github.com/settings/tokens,
# click "Generate token", and select "gist", "repo", "user", and "workflow" as scopes.

# Option 2
library(usethis)

create_github_token()

3

Next, we want to store the PAT for future use with R/RStudio.
The gitcreds package (which should be installed together with usethis) provides a function for this.
library(gitcreds)

gitcreds_set()

4

After we’ve made sure that we can use Git via RStudio and have created and stored a PAT for authentication via HTTPS, we can now clone and work on the GitHub repository we have created in the previous session. When doing this, you should also create a new RStudio project.
You can create a new version-controlled project that is connected to an existing GitHub repository via the RStudio menu. If you need some further information, you can check out the New project, GitHub first section in Happy Git and GitHub for the useR.
You can create a new version-controlled project and associate it with a repository cloned from GitHub via File -> New Project -> Version Control. Then choose Git, enter the URL of the GitHub repository (remember that we want to use HTTPS for authentication; if you want to use SSH, you need to provide a different URL), and choose a location where to store the project on your local machine. As we will start working on the project right away, also check “Open in new session”.

5

If everything has worked, a new instance of RStudio should have opened with the working directory set to the location of your new project, the files included in the project folder visible in the Files tab, and an active Git tab. To make extra sure that everything worked, let’s check the Git status of our project via the Terminal in RStudio.
You may want to check which shell the Terminal in RStudio uses via Tools -> Global Options -> Terminal. If you use Windows, you should choose Git Bash (which you should have installed with Git for Windows).
git status

6

Now that the project is set up and in sync with the remote GitHub repository, we can start working on it. Modify the README file (just add, remove or edit a few words). If you want to, you can also create a new folder called data and copy the file ZA5667_v1-0-0_CSV_synthetic-data.csv from the workshop materials there (either via the CLI/Terminal or by copying and pasting in your file explorer). This second step is optional. We will be using this in the following sessions, but you can also copy it then.

After modifying the file(s), save the changes (via the Save icon in the RStudio menu or the keyboard shortcut your OS uses for saving files) and stage them in Git.
You can use the RStudio GUI for staging changes (for modified or added files). As a reminder: When you modify existing files and/or create new ones and save the changes, these should be displayed in the Git tab in RStudio and their status will be indicated as modified or untracked.
You can stage changed files in the RStudio GUI by checking the boxes in the Staged column in the Git tab.

7

After staging your changes, let’s create a commit and push that to the remote GitHub repository.
You can do both (committing and pushing) via the RStudio GUI. Remember to write a meaningful commit message before you push.
After staging the changes, simply click on the Commit button in the Git tab in RStudio, write a commit message in the menu that opens up, and then click the Push button in the same menu.

8

As a final exercise for this session, let’s do the opposite of pushing and pull changes from GitHub to your local project. Before we can do this, we first need to make some edits in the remote repository. For that purpose, go to the website of your GitHub repository (while being logged in) and edit the README file in the browser (again, just add, remove, or edit a few words). You can do that via the small pen icon next displayed above the content of your README file. Once you have done that, you can pull the changes to your local project.
If you edit a file via the GitHub page, you also need to make a commit (and add a commit message) through that interface. You can do the pull operation via the respective icon in the RStudio GUI.
After making and saving the edits to the README file via the GitHub page for your repository, you can simply click the Pull button in the Git tab in RStudio to update your local project.